![]() ![]() |
BackgroundMac OS X 10.3 introduced a more optimized drawing model for Cocoa views. Supported by new NSView methods -getRectsBeingDrawn:count: and -needsToDrawRect: , this model enables a view to more precisely constrain its drawing to only those parts of its content area that need to be updated. Use of these new methods is discussed in detail in Drawing and Views subtopic Constraining Drawing to Improve Performance . The -needsToDrawRect: method is provided as a convenient means for a view's -drawRect: implementation to check whether some particular object in its content area needs to be drawn. Given the object's bounding rectangle, -needsToDrawRect: should return YES if this rectangle intersects any part of the view that needs redisplay. The ProblemAs an efficiency measure, NSView's -needsToDrawRect: implementation first tests whether the rectangle it is given intersects the bounding box of the area being drawn. In some cases -- most often when text is being drawn in the view -- this bounding rectangle can be computed incorrectly, which can cause -needsToDrawRect: to incorrectly return NO, and drawing that is conditioned on this result to be incorrectly suppressed. Overriding -needsToDrawRect: to Perform CorrectlyYou can work around this problem by overriding -needsToDrawRect: to perform as intended, in any custom view class(es) in which you use -needsToDrawRect:. If a view is rarely asked to draw more than a few rectangles at a time, and its content is not particularly complex or expensive to draw, the following reimplementation of -needsToDrawRect: will suffice.
The above approach has the advantage of not requiring any changes to code that calls -needsToDrawRect:. To improve performance in cases where a view is typically given many rectangles to draw at once, you may choose to define and use an alternative to this method that quickly rejects objects that lie outside the rectList's bounding rect. You would invoke this method instead of -needsToDrawRect:, passing it -drawRect:'s rectangle parameter as its "rectListBounds" parameter.
|
Developer Documentation | Technical Q&As | Development Kits | Sample Code |